<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Home\Controller;
use Think\Controller;

class DwzController extends Controller {

	//生成短连接地址
	public function short(){
		$this->display('Short/index');
	}
	/**
	 * 生成短链接
	 */
	public function creatshortlink(){
		$link = trim(I('post.link'));
		if(!empty($link)){
			$shortlink = M('shortlink');
			$res = $shortlink->where(array('y_link' => $link))->find();
			if($res){
				$this->assign('link', $res);
			}else{
				$date = date("Y-m-d H:i:m");
				$datalist['y_link'] = $link;
				$datalist['n_link'] = null;
				$datalist['date'] = $date;
				$res_c = $shortlink->add($datalist);
				if($res_c){
					$n_link = $this->savedwz($res_c);
				}
				$result = array(
					'id' => $res_c,
					'y_link' => $link,
					'n_link' => $n_link,
					'date' => $date
				);
				$this->assign('link', $result);
			}
		}
		$this->display('Short/index');
	}
	protected function savedwz($id = null){
		if(!empty($id)){
			$shortlink = M('shortlink');
			$l = C('WEBSITE').'/d/'.$id;
			$shortlink->n_link = $l;
			$shortlink->where(array('id' => $id))->save();
			return $l;
		}
	}

	/**
	 * 根据短网址输出原始网址
	 */
	public function dwz(){
		$dwzid = trim(I('get.dwzid'));
		if(is_numeric($dwzid)){
			$shortlink = M('shortlink');
			$res = $shortlink->find($dwzid);
			if($res){
				header("Location:".$res['y_link']);
			}else{
				echo "短网址有误";
			}
		}
	}

}